package com.ruoyi.project.product.info.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.product.info.domain.Info;
import com.ruoyi.project.product.info.service.InfoService;
import com.ruoyi.project.product.series.domain.Series;
import com.ruoyi.project.product.size.domain.Size;
import com.ruoyi.project.product.style.domain.Style;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Auther: 高柯
 * @Date: 2020/4/16
 * @Description: com.ruoyi.project.product.info.controller
 * @version: 1.0
 */
@Controller
@RequestMapping("/product/info")
public class InfoController extends BaseController {

    private String prefix = "product/info";

    @Autowired
    private InfoService infoService;

    @RequiresPermissions("product:info:view")
    @GetMapping()
    public String info(){
        return prefix + "/info";
    }

    /**
     * 获取产品系列信息
     * @return
     */
    @RequestMapping("/getSeries")
    @ResponseBody
    public List<Series> getSeries(){
        return this.infoService.getSeries();
    }

    /**
     * 获取产品风格信息
     * @return
     */
    @RequestMapping("/getStyle")
    @ResponseBody
    public List<Style> getStyle(){
        return this.infoService.getStyle();
    }

    /**
     * 获取产品规格信息
     * @return
     */
    @RequestMapping("/getSize")
    @ResponseBody
    public List<Size> getSize(){
        return this.infoService.getSize();
    }

    /**
     * 获取产品列表
     * @param info
     * @return
     */
    @RequiresPermissions("product:info:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Info info)
    {
        startPage();
        List<Info> list = infoService.selectInfoList(info);
        return getDataTable(list);
    }

    /**
     * 新增产品系列
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存产品系列
     */
    @RequiresPermissions("product:info:add")
    @Log(title = "产品管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated Info info)
    {
        if (UserConstants.SERIES_NAME_NOT_UNIQUE.equals(infoService.checkInfoNameUnique(info)))
        {
            return error("新增产品'" + info.getInfoName() + "'失败，产品名称已存在");
        }
        return toAjax(infoService.insertInfo(info));
    }

    /**
     * 校验产品系列名称
     */
    @PostMapping("/checkInfoNameUnique")
    @ResponseBody
    public String checkInfoNameUnique(Info info)
    {
        return infoService.checkInfoNameUnique(info);
    }

    /**
     * 修改产品
     */
    @GetMapping("/edit/{infoId}")
    public String edit(@PathVariable("infoId") Long infoId, ModelMap mmap)
    {
        mmap.put("info", infoService.selectInfoById(infoId));
        return prefix + "/edit";
    }

    /**
     * 修改保存产品
     */
    @RequiresPermissions("product:info:edit")
    @Log(title = "产品管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated Info info)
    {
        if (UserConstants.SERIES_NAME_NOT_UNIQUE.equals(infoService.checkInfoNameUnique(info)))
        {
            return error("修改产品'" + info.getInfoName() + "'失败，产品名称已存在");
        }
        return toAjax(infoService.updateInfo(info));
    }

    /**
     * 删除产品系列
     */
    @RequiresPermissions("product:info:remove")
    @Log(title = "产品管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        try
        {
            return toAjax(infoService.deleteInfoByIds(ids));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }
}
